package com.hz.foodstalls.controller;

import com.jfinal.weixin.sdk.api.ApiConfigKit;
import com.jfinal.weixin.sdk.api.ApiResult;
import com.jfinal.weixin.sdk.api.UserApi;
import com.jfinal.weixin.sdk.utils.JsonUtils;
import org.apache.commons.lang3.StringUtils;

import com.hz.foodstalls.interceptor.AdminInterceptor;
import com.hz.foodstalls.model.WechatInfo;
import com.hz.foodstalls.model.WxUser;
import com.jfinal.aop.Before;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.activerecord.Page;

import java.util.List;
import java.util.Map;

/**
 * WxUserController, Generated by Robot on 2017-5-10 10:51:09.
 */
@Before(AdminInterceptor.class)
@ControllerBind(controllerKey = "/wxuser", viewPath = "/WEB-INF/views/wxuser/")
public class WxUserController extends BaseController {

    private final String MODULE_NAME = "/wxuser";

    public void index() {
        String id = getPara("id");
        String errcode = getPara("errcode");
        String prjName = getPara("prjName");
            if(prjName == null){
                Page<WxUser> page = WxUser.dao.page(getParaToInt("pageNumber", 1), getParaToInt("pageSize", 10), id);
                setAttr("page", page);
            }else{
                Page<WxUser> page = WxUser.dao.page1(getParaToInt("pageNumber", 1), getParaToInt("pageSize", 10), id,prjName);
                setAttr("page", page);
            }

        WechatInfo wechatInfo = WechatInfo.dao.findFirst("select * from wechat_info where id=?", id);
        setAttr("id",id);
        if(errcode!=null && !"".equals(errcode)) {
            setAttr("errcode", getPara("errcode"));
        }
        setAttr("wx", wechatInfo);
        render("index.html");
    }

    public void save() {
        WxUser model = getModel(WxUser.class, "");
        boolean isSave = model.getId() == null;

        if (WxUser.dao.saveOrUpdate(model, isSave))
            redirect(MODULE_NAME);
        else
            renderError(501);
    }

    public void edit() {
        WxUser wxUser = WxUser.dao.findById(getParaToInt("id", 0));
        if (wxUser == null)
            wxUser = new WxUser();

        setAttr("wxUser", wxUser);
        render("edit.html");
    }

    public void delete() {
        String ids = StringUtils.join(getParaValues("ids"), ",");
        int ret = WxUser.dao.batchDelete(ids);
        if (ret > 0) {
            ajaxJsonSuccessMessage("删除成功!");
        } else {
            ajaxJsonErrorMessage("删除失败！");
        }
    }

    public void addOld(){
        String id = getPara("id");
        WechatInfo wechatInfo = WechatInfo.dao.findById(id);
        String appid = wechatInfo.getAppid();
        ApiConfigKit.setThreadLocalAppId(appid);
        ApiResult apiResult = UserApi.getFollows();
        if(apiResult.get("errcode")==null){
            Map map = apiResult.get("data");
            List<String> list =(List<String>) map.get("openid");
            //处理用户超过10000时
            if((int)apiResult.get("count")==10000){
                apiResult = UserApi.getFollowers((String) apiResult.get("next_openid"));
                map = apiResult.get("data");
                list.addAll((List<String>) map.get("openid"));
            }

            for(String user:list){
                WxUser.dao.saveWxUser(user,appid);
            }
            ApiConfigKit.removeThreadLocalAppId();
            redirect(MODULE_NAME+"?id="+id);
        }else{
            ApiConfigKit.removeThreadLocalAppId();
            redirect(MODULE_NAME+"?id="+id+"&errcode="+apiResult.get("errcode"));

        }

    }

}
